<template>
    <view style="background-color: #fff;position: fixed;width: 100%;height: 100%;">
        <view style="display:flex;justify-content:space-around;padding:12rpx;">
            <text class="recom" :class="[isShow==0?'active':'']" @click="choose(0)">推荐标题</text>
            <text class="recom" :class="[isShow==1?'active':'']" @click="choose(1)">自定义标题</text>
        </view>
        <view style="padding: 32rpx;" v-if="isShow==1">
            <view class="feature">
                <view class="add" @click="addCopy()">
                    <image style="width: 24rpx;height: 24rpx;margin-right: 6rpx;" src="https://www.diandianyouke.site:801/fileDataBase/static/stag/static/img/mov_icon_tjiazse.png" mode="" />
                    <text>添加新标题</text>
                </view>
                <text style="font-size: 28rpx;" :class="{'flag':isFlag}" @click="getIsFlag">{{isFlag?'完成':'管理'}}</text>
            </view>
            <view 
            class="items1" 
            v-for="item,index in titleArr" 
            :key="index" 
            :class="[itemIndex==index&&!isFlag?'choose-active':'']"
            @click="checked(index)"
            >
                <view class="top">
                    <text class="text-top" style="color: rgb(69,70,74);">标题{{ index+1 }}</text>
                    <view class="btn" v-if="isFlag">
                        <button class="edit" @click="editTitle(item.id,index)">编辑</button>
                        <button class="del" @click="delTitle(item.id)">删除</button>
                    </view>
                </view>
                <text class="text-bottom">{{ item.titleName }}</text>
                <image v-if="itemIndex==index&&!isFlag" class="img" src="https://www.diandianyouke.site:801/fileDataBase/static/stag/static/img/icon_xzong.png"/>
            </view>
            <!-- <view 
            class="items" 
            v-for="item,index in titleArr" 
            :key="item.id" 
            :class="[itemIndex==index?'checked':'']" 
            @click="checked(index)" >
                <view class="items-text" style="font-size: 28rpx;color: rgba(69,70,74,1);">{{item.titleName}}</view>
                <view class="min-bottom">
                    <button class="del" @click="delTitle(item.id)">删除</button>
                    <button class="compile" @click="editTitle(item.id,index)">编辑</button>
                </view>
            </view> -->
            <!-- <button class="btn" @click="addCopy()">添加自定义标题</button> -->
        </view>
        <view v-if="isShow==0" style="padding: 32rpx;margin-bottom: 16rpx;">
            <radio-group @change="radioChange">
            <label v-for="item,index in titleRed" :key="index" class="radio">
                <text>{{item.titleName}}</text>
                <radio color="rgba(142,84,244,1)" :value="item.titleName" />
            </label>
            </radio-group>
        </view>
    </view>
    <view style="height: 150rpx;">
        <view class="bottom">
                <!-- <button class="bottom-quxiao" @click="goCustom()">只用自定义标题</button> -->
                <button style="width: 690rpx;" class="bottom-queren" @click="queren()">确认</button>
        </view>
    </view>
    <uni-popup ref="popup" background-color="rgba(241,241,241,1)">
				<view class="popup-content">
                    <text class="text">自定义标题</text>
                    <textarea @input="areaInt" :value="titleName" class="input" placeholder="请输入自定义标题"/>
                </view>
                <view class="bottom">
                <button class="bottom-quxiao" @click="popupClose()">取消</button>
                <button class="bottom-queren" @click="addTitle()">确认</button>
            </view>
	</uni-popup>
    <uni-popup ref="popup1" background-color="rgba(241,241,241,1)">
				<view class="popup-content">
                    <text class="text">自定义标题</text>
                    <textarea :value="areaText" @input="textInt" class="input" placeholder="请输入自定义标题"/>
                </view>
                <view class="bottom">
                <button class="bottom-quxiao" @click="popupClose1()">取消</button>
                <button class="bottom-queren" @click="addTitle1()">确认</button>
            </view>
	</uni-popup>
</template>

<script>
import { httpRequest } from '../../utils/http';
export default {
    name: 'UniPresetVueVite1Title',

    data() {
        return {
            isShow:0,
            shopInfo:{},
            titleArr:[],
            titleName:'',
            flag:true,
            titleRed:[],
            id:0,
            areaText:'',
            current:0,
            diyTitleArr:'',
            changeText:'',
            index:-1,
            itemIndex:-1,
            isFlag:false
        };
    },

    onLoad(e) {
        this.shopInfo = uni.getStorageSync('shop')[e.index]
        this.getTitle()
        this.getTitleRed()  
    },
    methods: {
        getIsFlag(){
            this.isFlag = !this.isFlag
        },
        checked(i){
            this.itemIndex = i
            this.diyTitleArr = this.titleArr[i].titleName
        },
        queren(){
            if(this.isShow==0){
                uni.setStorageSync('title', this.changeText)
                uni.navigateBack({ delta: 1 }) 
            }else{
                uni.setStorageSync('title', this.diyTitleArr)
                uni.navigateBack({ delta: 1 })
            }
            
        },
        radioChange(e){
            this.changeText = e.detail.value
        },
        choose(n){
            this.isShow = n
        },
        addCopy(){
            this.$refs.popup.open('bottom')
            this.flag = true
        },
       async addTitle1(){
          const res =await uni.request({
                url: 'https://www.diandianyouke.cn:8081/api/editDiyTitle',
                data: {
                        id:this.id,
                        titleName:this.areaText,
                        shopId:this.shopInfo.shopId
                        },
                header: {
                    'Content-Type': 'application/json',
                },
                method: 'POST'
            })
        console.log(res);
        this.getTitle()
        this.$refs.popup1.close()
        
        },
        popupClose(){
        this.$refs.popup.close()
        this.intValue = ''
    },
    popupClose1(){
        this.$refs.popup1.close()
        this.intValue = ''
    },

    async getTitleRed(){
      const res = await httpRequest('/api/titleRad','POST',{shopCate2Id:this.shopInfo.shopCate2Obj.shopCate2Id})
        this.titleRed = res.data
    },
    async getTitle(){
        const res = await httpRequest('/api/getDiyTitle','POST',{shopId:this.shopInfo.shopId})
        console.log(res);
        this.titleArr = res.data
    },
    async delTitle(id){
        const res = await httpRequest('/api/delDiyTitle','POST',{id})
        this.getTitle()
    },
    async addTitle(id){
            const res = await httpRequest('/api/addDiyTitle','POST',{titleName:this.titleName,shopId:this.shopInfo.shopId})
            this.getTitle()
            this.$refs.popup.close()
        
    },
    async editTitle(id,index) {
        this.$refs.popup1.open('bottom')
        this.flag = true
        this.id = id
        this.areaText = this.titleArr[index].titleName
    },
    areaInt(e){
        this.titleName = e.detail.value.trim()
    },
    textInt(e){
        this.areaText = e.detail.value
    }
    },
};
</script>

<style lang="scss" scoped>
.checked{
    border: 1px solid rgba(142,84,244,1);
}
.recom{
        font-size: 28rpx;
        height: 110rpx;
        line-height: 110rpx;
        color: rgb(144,147,153);
    }
.items{
    height: 226rpx;
    padding: 16rpx;
    margin-bottom: 16rpx;
    background-color: rgba(249,249,249,1);
    .items-text{
        height: 50%;
        line-height: 112rpx;
        border-bottom: 2rpx solid rgb(220,223,230)
    }
    .min-bottom{
        display: flex;
        justify-content: flex-end;
        align-items: center;
        height: 50%;
        .del{
            width: 120rpx;
            height: 48rpx;
            font-size: 28rpx;
            text-align: center;
            line-height: 48rpx;
            margin: 0 16rpx 0 0;
            border: 1px solid rgba(255,55,71,1);
            color: rgba(255,55,71,1);
        }
        .compile{
            width: 120rpx;
            height: 48rpx;
            font-size: 28rpx;
            text-align: center;
            line-height: 48rpx;
            margin: 0;
            border: 2rpx solid rgba(69,70,74,1);
            color: rgba(69,70,74,1);

        }
    }
}
.items1{
    position: relative;
    padding: 24rpx;
    background-color: rgba(249,249,249,1);
    border-radius: 16rpx;
    margin-top: 24rpx;
    display: flex;
    flex-direction: column;
    border: 1px solid rgba(249,249,249,1);
    .text-top{
        font-size: 32rpx;
        color: rgba(255,55,71,1);
        font-weight: 700;
        margin-bottom: 12rpx;
    }
    .text-bottom{
        font-size: 28rpx;
        font-weight: 400;
        color: rgba(69,70,74,1);
    }
    .img{
        position: absolute;
        top: 0;
        right: 0;
        width: 72rpx;
        height: 48rpx;
    }
    .top{
        display: flex;
        justify-content: space-between;
        .btn{
            display: flex;
            justify-content: flex-end;
            .edit{
                width: 88rpx;
                height: 48rpx;
                text-align: center;
                line-height: 48rpx;
                font-size: 24rpx;
                color: rgba(69,70,74,1);
                background-color: rgb(241,241,241);
                padding: 0;
                margin: 0;
                margin-right: 24rpx;
            }
            .del{
                width: 88rpx;
                height: 48rpx;
                text-align: center;
                line-height: 48rpx;
                font-size: 24rpx;
                color: rgba(255,55,71,1);
                background-color: rgba(255,55,71,0.05);
                padding: 0;
                margin: 0;
            }
        }
    }
}
.radio{
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 130rpx;
    border-bottom: 1rpx solid rgba(220,223,230,1);
}
// .btn{
//     width: 686rpx;
//     height: 96rpx;
//     border-radius: 8rpx;
//     border: 2rpx dashed rgba(142,84,244,1);
//     text-align: center;
//     line-height: 96rpx;
//     margin-top: 20rpx;
// }
.bottom{
    position: fixed;
    bottom: 34rpx;
    left: 0;
    display: flex;
    justify-content: space-between;
    width: 100%;
    .bottom-quxiao{
    width: 330rpx;
    height: 86rpx;
    text-align: center;
    line-height: 88rpx;
    color: rgba(142,84,244,1);
    border: 1px solid rgba(142,84,244,1);
}
.bottom-queren{
    width: 332rpx;
    height: 88rpx;
    text-align: center;
    line-height: 88rpx;
    background-color: rgba(142,84,244,1);
    color: #fff;
    margin-right: 30rpx;
}
    }
    .active{
        color: rgba(142,84,244,1);
        border-bottom: 2px solid rgba(142,84,244,1);
        font-size: 32rpx;
    }
    .popup-content{
    height: 648rpx;
    padding: 24rpx 32rpx;
    .text{
        font-size: 32rpx;
        height: 96rpx;
        font-weight: 700;
    }
    .input{
        margin-top: 50rpx;
        background-color: #fff;
        height: 170rpx;
        width: 100%;
        border-radius: 16rpx;
    }
}
.feature{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 30rpx;
    .add{
        display: flex;
        justify-content: center;
        align-items: center;
        width: 252rpx;
        height: 64rpx;
        border: 2rpx solid rgba(142,84,244,1);
        font-size: 30rpx;
        color: rgba(142,84,244,1);
        border-radius: 8rpx;
    }
}
.flag{
    color: rgba(255,55,71,1) !important;
}
.choose-active{
    border: 1px solid rgba(142,84,244,1);
    background-color: rgba(142,84,244,0.05);
}
.choose-custom{
    border: 1px solid rgba(142,84,244,1);
}
</style>